/**
 * 
 */
var app = angular.module('iomApp', []);

var init = $(function() {
	initLayout();
	showFastMenu();
	initResize();
});


var initController = app.controller("indexController", function($scope, $http) {
	$scope.hasLogin = hasLogin;
	loginController($scope, $http);
	if (hasLogin) {
		showFastMenu();
		initMenuController($scope, $http);
		initLeftSidebarMenu();
	}

});

var initMenuController = function($scope, $http) {

	$scope.menus2 = [ {
		name : "1",
		child : [ {
			name : 2,
			child : [ {
				name : 3
			} ]
		} ]
	} ];

	$scope.showTabs = [ {
		name : '首页',
		code : 'homePage',
		href : 'Pages/monitorView/dashboard.jsp',
		closeAble : false,
		isActive : true,
		icon:'home icon'
	} ];

	$scope.showLeftSidebar = function() {
		$("#leftSidebar").sidebar('toggle');
	}
	$scope.showRightSidebar = function() {
		$("#rightSidebar").sidebar('toggle');
	}

	// 打开菜单操作
	$scope.openMenu = function() {
		/* openMenu(this.subMenu); */
		var showTabs = $scope.showTabs;
		var newMenu = this.subMenu;
		var isExist = false;
		for (var i = 0; i < showTabs.length; i++) {
			if (showTabs[i].code == newMenu.code) {
				// 如果存在,则说明已经打开，将该标签标记为激活
				isExist = true;
				showTabs[i].isActive = true;
			} else {
				showTabs[i].isActive = false;
			}
		}
		if (isExist) {
			// 如果存在,操作到此已结束
			return;
		}
		var tab = {
			name : newMenu.name,
			code : newMenu.code,
			href : newMenu.href,
			closeAble : true,
			isActive : true,
			icon:newMenu.icon
		};
		showTabs.push(tab);
	}

	$http({
		url : basePath + '/desktopAction/qryMenu',
		method : 'POST',
		params : {
			test : 'test1'
		}
	}).success(function(data, header, config, status) {
		// 响应成功
		$scope.menus = data;
	}).error(function(data, header, config, status) {
		// 处理响应失败
	});

	initXtabController($scope, $http);
}

var initXtabController = function($scope, $http) {
	$scope.changeTab = function() {
		var tabs = $scope.showTabs;
		for (var i = 0; i < tabs.length; i++) {
			if (tabs[i] == this.tab) {
				tabs[i].isActive = true;
			} else {
				tabs[i].isActive = false;
			}
		}
	}
	$scope.closeThisTab = function() {
		var spliceIndex = -1;
		var tabs = $scope.showTabs;
		var currentShowIndex = -1;
		var newShowIndex = -1;
		// 找出需要关闭tab的索引
		for (var i = 0; i < tabs.length; i++) {
			// 找出当前激活的tab索引
			if (tabs[i].isActive == true) {
				currentShowIndex = i;
				tabs[i].isActive = false;
			}
			if (tabs[i] == this.tab) {
				spliceIndex = i;
				// 如果关闭的标签是当前激活的标签，需要重新设置激活的tab
				// 否则不设置newShowIndex，即不是切换激活的tab
				if (currentShowIndex != i) {
					break;
				}
				if (i == tabs.length && i > 0) {
					// 如果关闭的tab是最后一个，则激活倒数第一个
					newShowIndex = i - 1;
				} else if (i > 0 && i < tabs.length - 1) {
					// 如果关闭的不是最后一个，则激活后面一个
					newShowIndex = i + 1;
				} else {
					newShowIndex = 0;
				}
			}
		}
		if (newShowIndex >= 0) {
			tabs[newShowIndex].isActive = true;
		} else {
			tabs[currentShowIndex].isActive = true;
		}

		if (spliceIndex >= 0) {
			// 从数组移除
			tabs.splice(spliceIndex, 1);
		}
	}
}

function initLayout() {
	var mainContainHeight = window.innerHeight - 48;
	var mainSegmentHeight = mainContainHeight - 54;
	$("#mainContent").height(mainContainHeight);
	$("#mainContent  .ui.segment").height(mainSegmentHeight);
}
function initLeftSidebarMenu() {
	$('#leftSidebarMenu').accordion({
		selector : {
			trigger : '.title'
		}
	});
}
function showFastMenu() {
	$('#headerMenu .browse.item').popup({
		inline : true,
		hoverable : true,
		position : 'bottom left',
		delay : {
			show : 200,
			hide : 500
		}
	})
}

function showLoginWindow() {
	$('#login-dimmer').dimmer('show');
}

function initResize() {

	$(window).resize(function() {
		initLayout();
	});
}

var loginController = function($scope, $http) {
	$scope.logout = function() {
		layer.confirm('确定要注销？？', {
			title:'温馨提示',
			btn : [ '确定', '取消' ]
		}, function() {

			$http({
				url : basePath + '/desktopAction/logout',
				method : 'POST'
			}).success(function(data, header, config, status) {
				// 响应成功
				$scope.hasLogin = false;
				window.history.go(0);
			}).error(function(data, header, config, status) {
				// 处理响应失败
			});

		}, function(index) {
			layer.close(index);
		});

	};

	// 展示登陆窗口
	$scope.showLogin = function() {
		showLoginWindow()
	}
	// 登陆动作
	$scope.login = function(a, b, c) {
		var param = {
			loginName : $scope.loginUser.username,
			password : $scope.loginUser.password,
			url : window.location.href
		};
		$.post(basePath + "/desktopAction/login", param, function(data) {
			var result = JSON.parse(data);
			if (result.code == 0) {
				// parent.layer.close(parent.loginWindowLayerIndex);
				layer.msg('登陆成功', {
					time : 1000
				}, function() {
					window.history.go(0)
				});
			} else {
				layer.msg(result.msg);
			}
		});
	}
	$scope.cancel = function() {
		$('#login-dimmer').dimmer('hide');

	}
	$scope.clear = function() {
		$('#login-dimmer').form('clear');
	}

	if (hasLogin) {
		return;
	} else {
		showLoginWindow();

	}
};